package cn.tannn.cat.file.sdk.exception;


import java.util.Objects;

/**
 * 自定义异常
 *
 * @author tn
 * @date 2021-01-22 14:15
 */
public class FileException extends RuntimeException {
    /**
     * 业务错误码
     */
    private Integer code;
    /**
     * 错误提示
     */
    private String message;

    public static String SYMBOL = "<=====>";

    /**
     * 空构造方法，避免反序列化问题
     */
    public FileException() {
        super();
    }

    private FileException(String message) {
        super(message);
        this.message = message;
        this.code = Integer.valueOf(message.substring(0, message.indexOf("<=====>")));
    }

    private FileException(String message, Throwable cause) {
        super(message, cause);
        this.message = message;
        this.code = Integer.valueOf(message.substring(0, message.indexOf("<=====>")));
    }

    public FileException(Throwable cause) {
        super(cause.getMessage(), cause);
        this.code = 500;
        this.message = cause.getMessage();
    }

    public FileException(FileExceptionCodeEnums errorCode) {
        super(errorCode.getMsg());
        this.code = errorCode.getCode();
        this.message = errorCode.getMsg();
    }

    public FileException(FileExceptionCodeEnums errorCode, Throwable cause) {
        super(errorCode.getMsg(), cause);
        this.code = errorCode.getCode();
        this.message = errorCode.getMsg();
    }

    public FileException(int code, String message, Throwable cause) {
        super(message, cause);
        this.message = message;
        this.code = code;
    }

    public FileException(int code, String message) {
        super(message);
        this.message = message;
        this.code = code;
    }


    /**
     * 特的message
     * <ps>用于无法使用BusinessException，当时想自定义失败code的情况(500)</ps>
     *
     * @param message message
     * @param cause   cause
     * @return code+ ExceptionResultWrap.symbol+message; (eg. 200<=====>你错了)
     */
    public static FileException specialMessage(String message, Throwable cause) {
        return new FileException(500 + SYMBOL + message, cause);
    }

    /**
     * 特的message
     * <ps>用于无法使用BusinessException，当时想自定义失败code的情况(500)</ps>
     *
     * @param message message
     * @return code+ ExceptionResultWrap.symbol+message; (eg. 200<=====>你错了)
     */
    public static FileException specialMessage(String message) {
        return new FileException(500 + SYMBOL + message);
    }

    /**
     * 特的message
     * <ps>用于无法使用BusinessException，当时想自定义失败code的情况</ps>
     *
     * @param code    code
     * @param message message
     * @return code+ ExceptionResultWrap.symbol+message; (eg. 200<=====>你错了)
     */
    public static FileException specialMessage(int code, String message) {
        return new FileException(code + SYMBOL + message);
    }

    /**
     * 特的message
     * <ps>用于无法使用BusinessException，当时想自定义失败code的情况</ps>
     *
     * @param code FileExceptionCodeEnums
     * @return code+ ExceptionResultWrap.symbol+message; (eg. 200<=====>你错了)
     */
    public static FileException specialMessage(FileExceptionCodeEnums code) {
        return new FileException(code.getCode() + SYMBOL + code.getMsg());
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof FileException)) {
            return false;
        }
        FileException that = (FileException) o;
        return Objects.equals(code, that.code) && Objects.equals(message, that.message);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code, message);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "FileException{" +
               "code=" + code +
               ", message='" + message + '\'' +
               '}';
    }
}
